#include <windows.h>
#include "Calculator.h"
#include <cstring>

using namespace std;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
	WNDCLASS wc;
	wc.lpszClassName = "CalculatorClass";
	wc.lpfnWndProc = MainWndProc;
	wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
	wc.lpszMenuName = "";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	RegisterClass(&wc);

	hWnd = CreateWindow(
		"CalculatorClass", 
		"Calculator", 
		WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, 
		CW_USEDEFAULT, 
		185, 
		265, 
		NULL, 
		NULL, 
		hInstance, 
		NULL);

	EditBox = CreateWindow(
		"EDIT", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, 
		10, 
		10, 
		155, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_Add = CreateWindow( 
		"BUTTON", 
		"+", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		10, 
		40, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	Button_Subtract = CreateWindow( 
		"BUTTON", 
		"-", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		50, 
		40, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	Button_Multiply = CreateWindow( 
		"BUTTON", 
		"*", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		90, 
		40, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	Button_Divide = CreateWindow( 
		"BUTTON", 
		"/", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		130, 
		40, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	Button_6 = CreateWindow( 
		"BUTTON", 
		"6", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		10, 
		80, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_7 = CreateWindow( 
		"BUTTON", 
		"7", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		50, 
		80, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_8 = CreateWindow( 
		"BUTTON", 
		"8", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		90, 
		80, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_9 = CreateWindow( 
		"BUTTON", 
		"9", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		130, 
		80, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_2 = CreateWindow( 
		"BUTTON", 
		"2", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		10, 
		120, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_3 = CreateWindow( 
		"BUTTON", 
		"3", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		50, 
		120, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_4 = CreateWindow( 
		"BUTTON", 
		"4", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		90, 
		120, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_5 = CreateWindow( 
		"BUTTON", 
		"5", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		130, 
		120, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_0 = CreateWindow( 
		"BUTTON", 
		"0", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		10, 
		160, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_1 = CreateWindow( 
		"BUTTON", 
		"1", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		50, 
		160, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_Decimal = CreateWindow( 
		"BUTTON", 
		".", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		90, 
		160, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_Equals = CreateWindow( 
		"BUTTON", 
		"=", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		130, 
		160, 
		35, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	Button_Clear = CreateWindow( 
		"BUTTON", 
		"Clear", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
		10, 
		200, 
		155, 
		25, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL);

	ShowWindow(hWnd, nCmdShow);
	ShowWindow(Button_9, nCmdShow);

//	EnableWindow(Button_9, FALSE);
//	ShowWindow(Button_9, SW_HIDE);
//	ShowWindow(Button_9, SW_SHOW);
//	DestroyWindow(Button_9);

	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0 )) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}

//----------------------------------------------------------------------------------

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, 
	WPARAM wParam, LPARAM lParam) 
{
	HWND hwndCtl = (HWND) lParam; 
	switch (msg) {
		case WM_COMMAND:
			switch (wParam) {
				case BN_CLICKED:
					if (hwndCtl == Button_1)
						InputNumber("1");
					else if (hwndCtl == Button_2)
						InputNumber("2");
					else if (hwndCtl == Button_3)
						InputNumber("3");
					else if (hwndCtl == Button_4)
						InputNumber("4");
					else if (hwndCtl == Button_5)
						InputNumber("5");
					else if (hwndCtl == Button_6)
						InputNumber("6");
					else if (hwndCtl == Button_7)
						InputNumber("7");
					else if (hwndCtl == Button_8)
						InputNumber("8");
					else if (hwndCtl == Button_9)
						InputNumber("9");
					else if (hwndCtl == Button_0)
						InputNumber("0");
					else if (hwndCtl == Button_Decimal)
						InputNumber(".");
					else if (hwndCtl == Button_Add) {
						Operation = '+';
						Number1 = atof(Num1);
					}
					else if (hwndCtl == Button_Subtract) {
						Operation = '-';
						Number1 = atof(Num1);
					}
					else if (hwndCtl == Button_Multiply) {
						Operation = '*';
						Number1 = atof(Num1);
					}
					else if (hwndCtl == Button_Divide) {
						Operation = '/';
						Number1 = atof(Num1);
					}
					else if (hwndCtl == Button_Equals) {
						Number2 = atof(Num2);
						Calculate();
					}
					else if (hwndCtl == Button_Clear) {
						Operation = '0';
						strcpy(Num1, "");
						Number1 = 0;
						strcpy(Num2, "");
						Number2 = 0;
						SetWindowText(EditBox, "");
					}
			}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

//----------------------------------------------------------------------------------

void InputNumber(char CurrentNumber[10]) 
{
	if (Operation == '0') 
	{
		strcat(Num1, CurrentNumber);
		SetWindowText(EditBox, Num1);
	}
	else 
	{
		strcat(Num2, CurrentNumber);
		SetWindowText(EditBox, Num2);
	}
}

//----------------------------------------------------------------------------------

void Calculate() 
{
	double Result = 0;
	char RES[25];
	
	switch(Operation)
	{
              case '+' : Result = Number1 + Number2; break;
              case '-' : Result = Number1 - Number2; break;
              case '*' : Result = Number1 * Number2; break;
              case '/' : Result = Number1 / Number2; break;
              default :  break;    
	}
    
	_gcvt(Result, 10, RES);
	SetWindowText(EditBox, RES);
	Operation = '0';
	strcpy(Num1, RES);
	Number1 = 0;
	strcpy(Num2, "");
	Number2 = 0;

}
//----------------------------------------------------------------------------------